package edu.hawaii.duedates;

import static org.junit.Assert.assertEquals;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.junit.Test;

/**
 * Implements testing of the BorrowedItem class.
 * 
 * @author Aric West
 */
public class TestBorrowedItem {
  List<String> list;
  BorrowedItem item;
  
  /**
   * Constructor.
   * 
   * @throws ParseException thrown if there was a problem regex searching the date
   */
  public TestBorrowedItem() throws ParseException {
    list = new ArrayList<String>();
    list.add("test1");
    list.add("01-01-08");
    item = new BorrowedItem(list);
  }

  /**
   * Testing getting the item name.
   */
  @Test
  public void testGetItemName() {
    assertEquals("Testing getting item name", item.getItemName(), list.get(0));
  }
  
  /**
   * Testing getting the due date as a Calendar object.
   * @throws ParseException thrown if there was a problem parsing the date string to a date object
   */
  @Test
  public void testGetDueDate() throws ParseException {
    Calendar date = Calendar.getInstance();
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yy", Locale.US);
    date.setTime(dateFormat.parse(list.get(1)));
    
    assertEquals("Testing getting item date", item.getDueDate(), date);
  }
  
  /**
   * Testing getting the due date as a String object.
   * @throws ParseException thrown if there was a problem parsing the date string to a date object
   */
  @Test
  public void testGetDueDateToString() throws ParseException {
    // required to convert from MM-dd-yy to MM-dd-yyyy
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yy", Locale.US);
    Date date = dateFormat.parse(list.get(1));
    dateFormat = new SimpleDateFormat("MM-dd-yyyy", Locale.US);
    
    assertEquals("Testing getting item date to string", item.getDueDateToString(),
      dateFormat.format(date));
  }
}
